home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 …SCII & the Runetime Code / ADC Developer CD (1992-07) (''Butch ASCII And The Runtime Code'')_iso / Dev.CD 199207.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / INIT - CDEV / SCN.xxx.INIT - CDEV < prev    next >
Encoding:
Text File  |  1991-01-22  |  6.7 KB  |  128 lines  |  [TEXT/MPS ]

  1. Macintosh
  2. Sample Code Notes
  3. _______________________________________________________________________________
  4.                                                     Developer Technical Support
  5.  
  6. #xx:    INIT - CDEV
  7.  
  8. Written by:        Keith Rollin
  9.  
  10. Versions:        1.00                        February 1991
  11.  
  12. Components:        CDEV.c                        C source
  13.                 CDEV.rsrc                    Misc resources
  14.                 Common.h                    C header for CDEV.c & INIT.c
  15.                 INIT - CDEV.make            Makefile
  16.                 INIT.a                        Asm source
  17.                 INIT.c                        C source
  18.                 INITInstall.a                Asm source
  19.                 SAGlobals.c                    C source
  20.                 SAGlobals.h                    C header
  21.                 SCN.xxx.INIT - CDEV            About file
  22.                 ShowINIT.a.o                ShowINIT library file (supplied as
  23.                                                 object file only, so don't
  24.                                                 delete it).
  25.  
  26. _______________________________________________________________________________
  27.  
  28. “INIT - CDEV” is a sample that shows how a CDEV can communicate with an INIT.
  29. It is a System 7.0 sample, as it uses the PPCToolbox. It also uses the
  30. FindFolder routine to show how to read and write a preferences file, and the
  31. stand-alone globals techniques described in Technote #256.
  32.  
  33. This sample demonstrates the following:
  34.  
  35. • How to write an INIT that can stay resident.
  36. • How to write a Control Panel Device with extended error reporting
  37. • How to patch a trap
  38. • How to use the FindFolder routine for managing preferences
  39. • How to do quick and dirty PPCToolbox communications
  40. • How to use global variables in standalone code
  41.  
  42.  
  43. The INIT is just a simple "do nothing" INIT. It patches into SystemTask so
  44. that it can beep a certain number of times at regular intervals. At startup
  45. time, it reads a preferences file to find out how many times it should beep,
  46. and how often. If there isn't a preferences file, or we are running under
  47. 6.0.x and don't have the luxery of the FindFolder routine, we use the
  48. hard-wired setting of beeping 3 times every minute. However, the INIT doesn't
  49. really do anything 7.0-ish, so it still installs at startup even if you are
  50. running under 6.0.x. It just won't communicate with the CDEV.
  51.  
  52. The CDEV can be used to change these settings. This CDEV runs under 7.0 only;
  53. it won't show up under 6.0.x. Also, it checks to see if the INIT was installed
  54. at startup time. If not, then it puts up an alert saying that it won't run
  55. unless the INIT is installed. There's no real good reason for doing this
  56. (i.e., you should be able to change the preferences file for the next time you
  57. reboot). But it shows how you can do your own error reporting in a CDEV.
  58.  
  59. The CDEV is based on the EditText CDEV sample. It's been modified slightly to
  60. communicate with the INIT. When it's brought up, you are presented with two
  61. edit text boxes. The first lets you set the number of times you beep. The
  62. second lets you set the interval in seconds. Nothing happens until you close
  63. the CDEV. When you do that, the CDEV uses the PPCToolbox to ask the INIT for a
  64. pointer to a public variables space. Once it has that pointer, it moves the
  65. new values into that space. The INIT then makes its decisions based on the new
  66. settings.
  67.  
  68.  
  69. About the files:
  70.  
  71. The code for the CDEV is in CDEV.c. This is based on EditCDEV.c in DTS sample
  72. code #10. It's been augmented with some more error checking, the routines
  73. needed for PPCToolbox communication, and some number <-> string conversion
  74. routines.
  75.  
  76. The code for the INIT is is INIT.c and INIT.a. Most of the meat, including the
  77. beeping and communication stuff, is in INIT.c. However, we use persistant
  78. global variables in the INIT (ala Technote #256), and have to keep around a
  79. reference to our globals during the time our SystemTask patch is not
  80. executing. To do this, we employ the services of a couple of routines in
  81. INIT.a. These routines, a_SetA5Ref and a_GetA5Ref, save and return our A5
  82. reference, which they store in a local DC.L. Also, the main entry point for
  83. our patch is in INIT.a. This entry point ("a_SystemTask") saves the registers,
  84. calls the C entry point ("c_SystemTask"), restores the registers, and then
  85. calls the original SystemTask in a way that doesn't consitute a tail patch.
  86.  
  87. The installation code for the INIT is in INITInstall.a. This code checks to
  88. see if the shift key or mouse button are pressed. If so, it skips the install.
  89. If not, then it attempts to get memory in the system heap for the patch. If it
  90. can't get it, then it skips the install. If it does get it, it moves the patch
  91. into that new block of memory, and calls some more installation code in the C
  92. routines. If that installation code indicates some sort of error, the block of
  93. memory is de-allocated, and we blow out. Otherwise, the patch to SystemTask is
  94. made. If there were any errors, we use ShowINIT to show an unhappy icon.
  95. Otherwise, we show a happy icon.
  96.  
  97. Because the CDEV and INIT exchange data back and forth, it's necessary for
  98. them to share some constants and data formats. These are defined in Common.h.
  99. The kind of stuff we consider common are message numbers and buffer formats.
  100. For instance, when we ask the INIT for the pointer to its common globals, we
  101. send it a message number "kGetCommonGlobalsPtr". This is defined in Common.h,
  102. so that we make sure that the value the INIT thinks is "kGetCommonGlobalsPtr"
  103. is the same value that the CDEV thinks it is. Similarly, the globals buffer
  104. format is defined in Commons.h. We also keep the name of the preferences file
  105. there, along with its file and creator type.
  106.  
  107. Now that ResEdit is so damn cool, we don't have a Rez source file. All of our
  108. resources are stored in CDEV.rsrc, and were created with ResEdit. These
  109. resources get merged into our final file with a command in the makefile.
  110.  
  111. Our INIT uses some global variables for convenience and so that we can have
  112. some persistant buffers for outstanding asynchronous PPCToolbox calls. To do
  113. this, we use the routines decribed in Technote #256, which are in the files
  114. SAGlobals.h and SAGlobals.c. However, we have modified them slightly for our
  115. own purposes. Some of the variables we will be using are things like parameter
  116. blocks for asynchronous PPCInform calls. Because the parameter block can't
  117. move out from under the PPCInform call, it needs to be in a non-relocatable
  118. block. Now, we could have just used the routines described in Technote #256
  119. and locked down our block of memory, but that's not a good idea. Our block of
  120. memory would need to be locked down all the time. Therefore, we want to it be
  121. in a part of the heap that can tolerate non-relocatable blocks. This means
  122. that it should go low in the heap. Without getting really tricky, we can't get
  123. a handle to go low in the heap. But there's no point; our globals will be
  124. locked ALL of the time, so we just allocate them with NewPtr. This has the
  125. added advantage making the Memory Manager put that block as low in the heap as
  126. it possibly can. So we modified the standard routines in SAGlobals.c to work
  127. with pointers and not handles.
  128.